﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DALScolarite;
namespace Scolarite 
{
    public partial class FicheEnseignants : Form
    {
        public modeload loadingmod;
        public long idenseignant = 0;

        public FicheEnseignants()
        {
            InitializeComponent();
        }

        private void btsave_Click(object sender, EventArgs e)
        {

             try
            {
            scolarite_enseignants addenseignant = new scolarite_enseignants
            {
               CIN = cINTextBox.Text,
               NomAR = nomARTextBox.Text,
               PrenomAR = prenomARTextBox.Text,
               NomFR = nomFRTextBox.Text,
               PrenomFR = prenomFRTextBox.Text,
               TelMobile = telMobileTextBox.Text

            };

           
             

            if (this.loadingmod == modeload.AJOUT)
            {
                GlobalParams.MonDbcontext.scolarite_enseignants.Add(addenseignant);
                GlobalParams.MonDbcontext.SaveChanges();
                MessageBox.Show("Nouveau enregistrement a été inseret");
            }
            else
            {

                uvtmoyEntities monentity = new uvtmoyEntities();
                monentity.scolarite_enseignants.Attach(addenseignant);
                monentity.Entry(addenseignant).State = EntityState.Modified;

                monentity.SaveChanges();

                // GlobalParams.MonDbcontext.SaveChanges();
                MessageBox.Show("enregistrement a été Modifier");
            }


            this.Close();


            }
             catch (DBConcurrencyException mes)
             {
                 MessageBox.Show(mes.Message);
             }
        }

        private void FicheEnseignants_Load(object sender, EventArgs e)
        {
            scolarite_domaine domaine = new scolarite_domaine();

 
                scolaritedomaineBindingSource.DataSource = domaine.domaineue();
            

            if (this.loadingmod == modeload.AJOUT)
            {
                  scolarite_enseignantsBindingSource.AddNew();
            }
            else if (this.loadingmod == modeload.INFO && this.idenseignant != 0)
            {
                scolarite_enseignantsBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_enseignants.Where(et => et.idEnseignants == idenseignant).ToList();

            }
        }

        private void btselectdomaine_Click(object sender, EventArgs e)
        {
            List<scolarite_domaine> grp = new List<scolarite_domaine>();


            for (int x = listBox1.Items.Count - 1; x >= 0; x--)
            {
                if (listBox1.GetSelected(x))
                {

                    listBox2.Items.Add(listBox1.Items[x]);

                    // listBox1.Items.RemoveAt(idx);
                }
                else
                {
                    grp.Add((scolarite_domaine)listBox1.Items[x]);
                }
            }


            listBox2.DisplayMember = "labelfr";
            listBox2.ValueMember = "idScolarite_Domaine";


            listBox1.DataSource = null;
            listBox1.Items.Clear();
            foreach (var elt in grp)
            {
                listBox1.Items.Add(elt);
            }

            listBox1.DisplayMember = "labelfr";
            listBox1.ValueMember = "idScolarite_Domaine";
        }

        private void btdeselectdomaine_Click(object sender, EventArgs e)
        {
            List<scolarite_domaine> grp = new List<scolarite_domaine>();


            for (int x = listBox2.Items.Count - 1; x >= 0; x--)
            {
                if (listBox2.GetSelected(x))
                {
                    listBox1.Items.Add(listBox2.Items[x]);
                }
                else
                {
                    grp.Add((scolarite_domaine)listBox2.Items[x]);
                }
            }


            listBox1.DisplayMember = "labelfr";
            listBox1.ValueMember = "idScolarite_Domaine";

            listBox2.Items.Clear();
            foreach (var elt in grp)
            {
                listBox2.Items.Add(elt);
            }

            listBox2.DisplayMember = "labelfr";
            listBox2.ValueMember = "idScolarite_Domaine";
        }
    }
}
